package com.konsung.ksdc.manager.machine;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.konsung.ksdc.R;
import com.konsung.ksdc.bean.netbean.MachineNetBean;
import com.konsung.ksdc.common.utils.DateUtils;
import com.konsung.ksdc.common.utils.StringUtils;
import com.konsung.ksdc.config.MyApplication;

import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 关联的一体机适配器
 * Created by DLX on 2016/6/13 0013.
 */
public class MacDetailAdapter extends PagerAdapter {
    @Bind(R.id.tv_machine_name)
    TextView tvMachineName;
    @Bind(R.id.tv_product_time)
    TextView tvProductTime;
    @Bind(R.id.tv_product_addr)
    TextView tvProductAddr;
    @Bind(R.id.tv_machine_serialnum)
    TextView tvMachineSerialnum;
    @Bind(R.id.tv_machine_type)
    TextView tvMachineType;
    @Bind(R.id.tv_machine_deadline)
    TextView tvMachineDeadline;
    private List<MachineNetBean> machineNetBeanList;
    private final LayoutInflater inflater;

    /**
     * 构造方法
     * @param machineList 关联的一体机集合
     */
    public MacDetailAdapter(List<MachineNetBean> machineList) {
        this.machineNetBeanList = machineList;

        Context context = MyApplication.getContext();
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        if (machineNetBeanList != null) {
            return machineNetBeanList.size();
        }
        return 0;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = inflater.inflate(R.layout.item_vp_mac_detail, null);
        ButterKnife.bind(this, view);

        MachineNetBean bean = machineNetBeanList.get(position);
        tvMachineName.setText(bean.getMacName());
        tvMachineSerialnum.setText(bean.getSerialNum());
        tvProductAddr.setText(bean.getProcAddr());

        String warrantyDate = bean.getWarrantyDate();
        String procDate = bean.getProcDate();

        // 日期格式化
        tvMachineDeadline.setText(parseDate(warrantyDate));
        tvProductTime.setText(parseDate(procDate));
        tvMachineType.setText(bean.getMacStyle());

        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    /**
     * 转换日期
     * @param dateString 字符串
     * @return 日期
     */
    private String parseDate(String dateString) {
        if (StringUtils.isEmpty(dateString)) {
            return "";
        }
        try {
            long dateLong = Long.parseLong(dateString);
            Date date = new Date();
            date.setTime(dateLong);
            return DateUtils.getStringDateFormat(date, DateUtils.yyyyMmDd);
        } catch (Exception e) {
            return "";
        }
    }
}
